import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/extension/extension_string.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/common/xindong_text_field.dart';

enum XinDongInputProfileEnum {
  city,
  school_of_graduation,
  name,
  remark,
  wechat,
  qq,
  inviteCode,
}

class XinDongInputTextYeMian extends StatelessWidget {
  final XinDongInputProfileEnum inputEnum;
  final String? value;
  final Function(String text) doneCallback;

  const XinDongInputTextYeMian({Key? key, required this.inputEnum, required this.value, required this.doneCallback}) : super(key: key);

  String get title {
    switch (inputEnum) {
      case XinDongInputProfileEnum.city:
      case XinDongInputProfileEnum.school_of_graduation:
        return "请填写";
      case XinDongInputProfileEnum.name:
        return "编辑昵称";
      case XinDongInputProfileEnum.remark:
        return "编辑备注";
      case XinDongInputProfileEnum.wechat:
        return "编辑微信";
      case XinDongInputProfileEnum.qq:
        return "编辑备用QQ/微信";
      case XinDongInputProfileEnum.inviteCode:
        return "编辑邀请码";
      default:
        return "";
    }
  }

  String get placeholder {
    switch (inputEnum) {
      case XinDongInputProfileEnum.city:
        return "请填写所在地城市";
      case XinDongInputProfileEnum.school_of_graduation:
        return "请填写毕业院校";
      case XinDongInputProfileEnum.name:
        return "请填写昵称1～12字";
      case XinDongInputProfileEnum.remark:
        return "请填写备注";
      case XinDongInputProfileEnum.wechat:
        return "请填写真实的微信号码，并允许微信好友搜索";
      case XinDongInputProfileEnum.qq:
        return "请填写真实的QQ/微信";
      case XinDongInputProfileEnum.inviteCode:
        return "请填写邀请码（选填）";
      default:
        return "";
    }
  }

  TextInputType get textInputType {
    switch (inputEnum) {
      case XinDongInputProfileEnum.wechat:
      case XinDongInputProfileEnum.qq:
      case XinDongInputProfileEnum.inviteCode:
        return TextInputType.emailAddress;
      default:
        return TextInputType.text;
    }
  }

  @override
  Widget build(BuildContext context) {
    final TextEditingController editController = TextEditingController(text: value);
    return Scaffold(
      appBar: XinDongAppBar(title),
      body: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: ListView(
          children: [
            const SizedBox(height: 50),
            Container(
              height: kCellHeight,
              color: DZColors.cellColor,
              alignment: Alignment.center,
              child: XinDongTextField(
                textAlign: TextAlign.start,
                hintText: placeholder,
                controller: editController,
                contentPadding: const EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
                keyboardType: textInputType,
                inputFormatters: [LengthLimitingTextInputFormatter(inputEnum == XinDongInputProfileEnum.name ? 12 : 20)],
              ),
            ),
            const SizedBox(height: 50),
            Container(
              height: kButtonHeight,
              padding: const EdgeInsets.only(left: 16, right: 16),
              child: XinDongCommonButton(
                "确定",
                onTap: () {
                  String text = editController.text.trim();
                  if (inputEnum == XinDongInputProfileEnum.wechat || inputEnum == XinDongInputProfileEnum.qq) {
                    text = text.trimSpaceAndLinefeed;
                  }
                  doneCallback(text);
                  Navigator.pop(context, text);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
